package com.lindroy.androidplayer.logic.network2

import com.king.retrofit.retrofithelper.RetrofitHelper
import com.lindroy.androidplayer.constants.BASE_URL
import okhttp3.OkHttpClient
import retrofit2.Retrofit

/**
 * @author Lin
 * @date 2021/4/15
 * @function 网络请求构建器基类
 */
abstract class BaseNetworkService {

    fun <T> create(serviceClass: Class<T>): T{
        val retrofitBuilder = Retrofit.Builder()
            .baseUrl(BASE_URL)
            .client(okHttpClient)
        return setRetrofitBuilder(retrofitBuilder).build().create(serviceClass)
    }


    /**
     * 实现重写父类的setHttpClientBuilder方法，
     * 在这里可以添加拦截器，可以对 OkHttpClient.Builder 做任意操作
     */
    abstract fun setHttpClientBuilder(builder: OkHttpClient.Builder): OkHttpClient.Builder

    /**
     * 实现重写父类的setRetrofitBuilder方法，
     * 在这里可以对Retrofit.Builder做任意操作，比如添加GSON解析器，Protocol
     */
    abstract fun setRetrofitBuilder(builder: Retrofit.Builder): Retrofit.Builder

    /**
     * 配置http
     */
    private val okHttpClient: OkHttpClient
        get() {
            //RetrofitUrlManager可以支持多BaseUrl
            //            OkHttpClient.Builder().
            return RetrofitHelper.getInstance().createClientBuilder().apply {
                setHttpClientBuilder(this)
            }.build()
        }
}